<template>
  <el-dialog
    :title="!dataForm.columnId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="归属表编号" prop="tableId">
      <el-input v-model="dataForm.tableId" placeholder="归属表编号"></el-input>
    </el-form-item>
    <el-form-item label="列名称" prop="columnName">
      <el-input v-model="dataForm.columnName" placeholder="列名称"></el-input>
    </el-form-item>
    <el-form-item label="列描述" prop="columnComment">
      <el-input v-model="dataForm.columnComment" placeholder="列描述"></el-input>
    </el-form-item>
    <el-form-item label="列类型" prop="columnType">
      <el-input v-model="dataForm.columnType" placeholder="列类型"></el-input>
    </el-form-item>
    <el-form-item label="JAVA类型" prop="javaType">
      <el-input v-model="dataForm.javaType" placeholder="JAVA类型"></el-input>
    </el-form-item>
    <el-form-item label="JAVA字段名" prop="javaField">
      <el-input v-model="dataForm.javaField" placeholder="JAVA字段名"></el-input>
    </el-form-item>
    <el-form-item label="是否主键（1是）" prop="isPk">
      <el-input v-model="dataForm.isPk" placeholder="是否主键（1是）"></el-input>
    </el-form-item>
    <el-form-item label="是否自增（1是）" prop="isIncrement">
      <el-input v-model="dataForm.isIncrement" placeholder="是否自增（1是）"></el-input>
    </el-form-item>
    <el-form-item label="是否必填（1是）" prop="isRequired">
      <el-input v-model="dataForm.isRequired" placeholder="是否必填（1是）"></el-input>
    </el-form-item>
    <el-form-item label="是否为插入字段（1是）" prop="isInsert">
      <el-input v-model="dataForm.isInsert" placeholder="是否为插入字段（1是）"></el-input>
    </el-form-item>
    <el-form-item label="是否编辑字段（1是）" prop="isEdit">
      <el-input v-model="dataForm.isEdit" placeholder="是否编辑字段（1是）"></el-input>
    </el-form-item>
    <el-form-item label="是否列表字段（1是）" prop="isList">
      <el-input v-model="dataForm.isList" placeholder="是否列表字段（1是）"></el-input>
    </el-form-item>
    <el-form-item label="是否查询字段（1是）" prop="isQuery">
      <el-input v-model="dataForm.isQuery" placeholder="是否查询字段（1是）"></el-input>
    </el-form-item>
    <el-form-item label="查询方式（等于、不等于、大于、小于、范围）" prop="queryType">
      <el-input v-model="dataForm.queryType" placeholder="查询方式（等于、不等于、大于、小于、范围）"></el-input>
    </el-form-item>
    <el-form-item label="显示类型（文本框、文本域、下拉框、复选框、单选框、日期控件）" prop="htmlType">
      <el-input v-model="dataForm.htmlType" placeholder="显示类型（文本框、文本域、下拉框、复选框、单选框、日期控件）"></el-input>
    </el-form-item>
    <el-form-item label="字典类型" prop="dictType">
      <el-input v-model="dataForm.dictType" placeholder="字典类型"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          columnId: 0,
          tableId: '',
          columnName: '',
          columnComment: '',
          columnType: '',
          javaType: '',
          javaField: '',
          isPk: '',
          isIncrement: '',
          isRequired: '',
          isInsert: '',
          isEdit: '',
          isList: '',
          isQuery: '',
          queryType: '',
          htmlType: '',
          dictType: '',
          sort: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: ''
        },
        dataRule: {
          tableId: [
            { required: true, message: '归属表编号不能为空', trigger: 'blur' }
          ],
          columnName: [
            { required: true, message: '列名称不能为空', trigger: 'blur' }
          ],
          columnComment: [
            { required: true, message: '列描述不能为空', trigger: 'blur' }
          ],
          columnType: [
            { required: true, message: '列类型不能为空', trigger: 'blur' }
          ],
          javaType: [
            { required: true, message: 'JAVA类型不能为空', trigger: 'blur' }
          ],
          javaField: [
            { required: true, message: 'JAVA字段名不能为空', trigger: 'blur' }
          ],
          isPk: [
            { required: true, message: '是否主键（1是）不能为空', trigger: 'blur' }
          ],
          isIncrement: [
            { required: true, message: '是否自增（1是）不能为空', trigger: 'blur' }
          ],
          isRequired: [
            { required: true, message: '是否必填（1是）不能为空', trigger: 'blur' }
          ],
          isInsert: [
            { required: true, message: '是否为插入字段（1是）不能为空', trigger: 'blur' }
          ],
          isEdit: [
            { required: true, message: '是否编辑字段（1是）不能为空', trigger: 'blur' }
          ],
          isList: [
            { required: true, message: '是否列表字段（1是）不能为空', trigger: 'blur' }
          ],
          isQuery: [
            { required: true, message: '是否查询字段（1是）不能为空', trigger: 'blur' }
          ],
          queryType: [
            { required: true, message: '查询方式（等于、不等于、大于、小于、范围）不能为空', trigger: 'blur' }
          ],
          htmlType: [
            { required: true, message: '显示类型（文本框、文本域、下拉框、复选框、单选框、日期控件）不能为空', trigger: 'blur' }
          ],
          dictType: [
            { required: true, message: '字典类型不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.columnId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.columnId) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/gentablecolumn/info/${this.dataForm.columnId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tableId = data.genTableColumn.tableId
                this.dataForm.columnName = data.genTableColumn.columnName
                this.dataForm.columnComment = data.genTableColumn.columnComment
                this.dataForm.columnType = data.genTableColumn.columnType
                this.dataForm.javaType = data.genTableColumn.javaType
                this.dataForm.javaField = data.genTableColumn.javaField
                this.dataForm.isPk = data.genTableColumn.isPk
                this.dataForm.isIncrement = data.genTableColumn.isIncrement
                this.dataForm.isRequired = data.genTableColumn.isRequired
                this.dataForm.isInsert = data.genTableColumn.isInsert
                this.dataForm.isEdit = data.genTableColumn.isEdit
                this.dataForm.isList = data.genTableColumn.isList
                this.dataForm.isQuery = data.genTableColumn.isQuery
                this.dataForm.queryType = data.genTableColumn.queryType
                this.dataForm.htmlType = data.genTableColumn.htmlType
                this.dataForm.dictType = data.genTableColumn.dictType
                this.dataForm.sort = data.genTableColumn.sort
                this.dataForm.createBy = data.genTableColumn.createBy
                this.dataForm.createTime = data.genTableColumn.createTime
                this.dataForm.updateBy = data.genTableColumn.updateBy
                this.dataForm.updateTime = data.genTableColumn.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/gentablecolumn/${!this.dataForm.columnId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'columnId': this.dataForm.columnId || undefined,
                'tableId': this.dataForm.tableId,
                'columnName': this.dataForm.columnName,
                'columnComment': this.dataForm.columnComment,
                'columnType': this.dataForm.columnType,
                'javaType': this.dataForm.javaType,
                'javaField': this.dataForm.javaField,
                'isPk': this.dataForm.isPk,
                'isIncrement': this.dataForm.isIncrement,
                'isRequired': this.dataForm.isRequired,
                'isInsert': this.dataForm.isInsert,
                'isEdit': this.dataForm.isEdit,
                'isList': this.dataForm.isList,
                'isQuery': this.dataForm.isQuery,
                'queryType': this.dataForm.queryType,
                'htmlType': this.dataForm.htmlType,
                'dictType': this.dataForm.dictType,
                'sort': this.dataForm.sort,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
